package cn.vcjf.washcar.common.util;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWTSigner;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.internal.com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zhengzihang
 * @Date: 2018/7/20 18:03
 * @Description:
 */

public class JWT {
	private static final String SECRET = "XX#$%()(#*!()!KL<><MQLMNQNQJQK sdfkjsdrow32234545fdf>?N<:{LWPW";

	private static final String EXP = "exp";

	private static final String PAYLOAD = "payload";

	public static String EXPIRE = "EXP";

	public static String REFLASHTOKEN = "REFLASHTOKEN";

	//加密，传入一个对象和有效期
		public static <T> String sign(T object, long maxAge) {
		try {
			final JWTSigner signer = new JWTSigner(SECRET);
			final Map<String, Object> claims = new HashMap<String, Object>();
			ObjectMapper mapper = new ObjectMapper();
			String jsonString = mapper.writeValueAsString(object);
			claims.put(PAYLOAD, jsonString);
			claims.put(EXP, System.currentTimeMillis() + maxAge);
			return signer.sign(claims);
		} catch(Exception e) {
			return null;
		}
	}

	//解密，传入一个加密后的token字符串和解密后的类型
	public static<T> T unsign(String jwt, Class<T> classT) {
		final JWTVerifier verifier = new JWTVerifier(SECRET);
		try {
			final Map<String,Object> claims= verifier.verify(jwt);
			if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)) {
				long exp = (Long)claims.get(EXP);
				long currentTimeMillis = System.currentTimeMillis();
				if (exp > currentTimeMillis) {
					String json = (String)claims.get(PAYLOAD);
					T obj = JSONObject.parseObject(json, classT);
					return obj;
				}
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	//解密，传入一个加密后的token字符串和解密后的类型
	public static<T> String checkSign(String jwt, Class<T> classT) {
		final JWTVerifier verifier = new JWTVerifier(SECRET);
		try {
			final Map<String,Object> claims= verifier.verify(jwt);
			if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)) {
				long exp = (Long)claims.get(EXP);
				long currentTimeMillis = System.currentTimeMillis();
				if (exp > currentTimeMillis) {
					String json = (String)claims.get(PAYLOAD);
					ObjectMapper objectMapper = new ObjectMapper();
					T obj = JSONObject.parseObject(json, classT);
					System.out.println(obj.toString());
					return REFLASHTOKEN;
				}else {
					return EXPIRE;
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}

	public static int isExpire(String string) {
		if(EXPIRE.equals(string)){
			return 0;
		}
		if(REFLASHTOKEN.equals(string)){
			return 1;
		}
		return -1;
	}
}
